from database.db_handler import MysqlHander

class EntityData:
    def __init__(self):
        self.db = MysqlHander("config.ini")
        pass

    def del_entity_data(self, name):
        update = "delete from db_entity_set_data where Fname='" + name + "';"
        
        print(update)
        try:
            #pass
            self.db.execute_not_safe(update)
        except Exception as e:
            print(str(e))
            pass

    def add_entity_data(self, name, desc, class1, class2, pre_attr, post_attr, entity_set):
        #insert = "insert into market_data (Fdate, Ftype, Ftitle, Furl, Fsite, Fcreate_time, Fmodify_time) values "
        insert = "insert into db_entity_set_data (Fname,Fdesc,Fclass1,Fclass2,Fpre_attr,Fpost_attr,Fentity_set,Fstatus,Fcreate_time,Fmodify_time)"
        insert += " values ('" + name + "', '" + desc + "'," + str(class1) + ", " + str(class2) + ",'" + pre_attr + "','" + post_attr
        insert += "','" + entity_set + "',0, now(), now());"
        
        print(insert)
        try:
            #pass
            self.db.execute_not_safe(insert)
        except Exception as e:
            print(str(e))
            pass


    def update_entity_data(self, name, desc, class1, class2, pre_attr, post_attr, entity_set):
        #insert = "insert into market_data (Fdate, Ftype, Ftitle, Furl, Fsite, Fcreate_time, Fmodify_time) values "
        update = "update db_entity_set_data set Fdesc='" + desc +"',Fclass1=" + str(class1) + ",Fclass2=" + str(class2) + ","
        update += "Fpre_attr='" + pre_attr + "',Fpost_attr='" + post_attr + "',Fentity_set='" + entity_set + "',Fmodify_time=now() where Fname='" + name + "';"
        
        print(update)
        try:
            #pass
            self.db.execute_not_safe(update)
        except Exception as e:
            print(str(e))
            pass

    def update_set_by_id(self, id, entity_set):
        update = "update db_entity_set_data set Fentity_set='" + entity_set + "',Fmodify_time=now() where Fid=" + str(id) + ";"
        
        print(update)
        try:
            #pass
            self.db.execute_not_safe(update)
        except Exception as e:
            print(str(e))
            pass

    def query_entity_for_index(self, id):
        query = "select Fid, Fname, Fentity_set from db_entity_set_data where Fid > " + str(id) + " order by Fid asc  limit 2000;"
        
        print(query)
        return self.db.query_sql(query)

